import os

from random import choice
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

onMenu = [["/images/small/american_burger.png", "All American Smashburger", "4.99", "5.99"],
            ["/images/small/avacado.png", "Avacado Club Smashchicken", "6.99"],
            ["/images/small/bacon.png", "BBQ Ranch Smashchicken", "6.99"],
            ["/images/small/beef.png", "Beefy Chili", "2.59", "3.59", "S"],
            ["/images/small/chicken_sandwich.png", "Classic Smashchicken", "5.99", "6.99"],
            ["/images/small/classic_smashburger.png", "Classic Smashburger", "5.99", "6.99"],
            ["/images/small/fries.png", "Smashfries", "1.99", "2.99", "S"],
            ["/images/small/hot_sauce.png", "Buffalo Smashchicken", "5.99"],
            ["/images/small/jalepeno.png", "Spicy Baja Smashburger", "5.99", "6.99"],
            ["/images/small/mushroom.png", "Mushroom Swiss Smashburger", "5.99", "6.99"],
            ["/images/small/onion.png", "Haystack Onions", "1.99", "2.99", "S"],
            ["/images/small/pickles.png", "Fried Pickles", "1.99", "2.99", "S"],
            ["/images/small/potato.png", "Smashfries", "1.99", "2.99", "S"],
            ["/images/small/veggies.png", "Veggie Frites", "2.99", "3.99", "S"]];

topics = ["BUN", "CHEESE", "SAUCE", "TOPPINGS","Add's","With", "Beverages"];
makeYourOwn = [["BUN", "Classic Burger Bun", "0.00"],
               ["BUN", "Multigrain Bun", "0.00"],
               ["BUN", "Chipotle Bun", "0.00"],
               ["BUN", "No Burger Bun", "0.00"],
               ["BUN", "Onion Bun", "0.00"],
               ["CHEESE", "American Cheese", "0.00"],
               ["CHEESE", "Swiss Cheese", "0.00"],
               ["CHEESE", "Sharp Cheddar", "0.00"],
               ["CHEESE", "Blue Cheese", "0.00"],
               ["CHEESE", "Bar Cheese", "0.00"],
               ["CHEESE", "Pepper Jack", "0.00"],
               ["SAUCE", "Ketchup", "0.00"],
               ["SAUCE", "Mayonaisse", "0.00"],
               ["SAUCE", "Mustard", "0.00"],
               ["SAUCE", "Ranch Dressing", "0.00"],
               ["SAUCE", "Smash Sauce", "0.00"],
               ["SAUCE", "Spicy Mustard", "0.00"],
               ["TOPPINGS", "Jalapenos", "0.00"],
               ["TOPPINGS", "Lettuce", "0.00"],
               ["TOPPINGS", "Pickle", "0.00"],
               ["TOPPINGS", "Red Onion", "0.00"],
               ["TOPPINGS", "Romaine", "0.00"],
               ["TOPPINGS", "Tomato", "0.00"],
               ["TOPPINGS", "Relish", "0.00"],
               ["TOPPINGS", "No Avocado", "0.99"],
               ["TOPPINGS", "Guacamole", "0.99"],
               ["Add's", "Fried Egg", "0.99"],
               ["Add's", "American Cheese", "0.49"],
               ["Add's", "Blue Cheese", "0.49"],
               ["Add's", "Cheddar", "0.49"],
               ["Add's", "Swiss Cheese", "0.49"],
               ["Add's", "Chili", "0.99"],
               ["Add's", "Guacamole", "0.99"],
               ["Add's", "Bacon", "1.49"],
               ["Add's", "1/3 lb Beef Burger", "2.00"],
               ["Add's", "1/2 lb Beef Burger", "3.00"],
               ["Add's", "Chicken Breast", "1.79"],
               ["Add's", "Mushrooms", "0.99"],
               ["Add's", "Garlic Onion", "0.49"],
               ["Add's", "Bar Cheese", "0.49"],
               ["Add's", "Garlic", "0.49"],
               ["Add's", "Avocado", "0.99"],
               ["With", "French Fries", "1.69"],
               ["With", "Veg Frites", "2.99"],
               ["With", "Angus Chili", "2.59"],
               ["With", "Haystack Onions", "1.99"],
               ["With", "Smash Fries", "1.99"],
               ["With", "Sweet Potato Fries", "1.99"],
               ["With", "Fried Pickles", "1.99"],
               ["Beverages", "Fountain Drink", "1.79"],
               ["Beverages", "Iced Tea", "1.79"],
               ["Beverages", "Bottled Water", "1.79"],
               ["Beverages", "Milk", "0.99"],
               ["Beverages", "Milkshake", "3.99"],
               ["Beverages", "Malt", "3.99"],
               ["Beverages", "Rootbeer Float", "3.99"],
               ["Beverages", "IBC Rootbeer", "1.79"]]

signin = ["Who <i>are</i> you?!? Are you <a target=\"_blank\" href=\"http://thirdeyedumb.files.wordpress.com/2008/05/strangeguy.jpg\"><i>this</i></a> guy? Log in!",
         "Try signing in! It's fun! It's free! It lets me stalk you later!",
         "If you like to eat, you're in good <a target=\"_blank\" href='http://www.youtube.com/watch?v=VATmgtmR5o4'>company!</a>"];
    
class Meal(db.Model):
    author = db.UserProperty()
    name = db.StringProperty(multiline=True)
    details = db.StringProperty(multiline=True)
    cost = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    
class SmashBurgerer(db.Model):
    user = db.UserProperty()
    name = db.StringProperty(multiline=True)
    
class Error(db.Model):
    number = db.StringProperty()
    message = db.StringProperty()

class MainPage(webapp.RequestHandler):
    def get(self):
        burgers_query = Meal.all().order('-date')
        burgers = burgers_query.fetch(1000)
        allUsers_query = SmashBurgerer.gql("WHERE user=:1",users.get_current_user())
        allUsers = allUsers_query.fetch(1,0)
            
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'
        
        template_values = {
            'pastburgers': burgers,
            'url': url,
            'signin': choice(signin),
            'allUsers': allUsers,
            'user': users.get_current_user(),
            'onMenu': onMenu,
            'makeYourOwn': makeYourOwn,
            'topics': topics,
            'error': self.request.get("error"),
            'url_linktext': url_linktext,
            }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class AddBurger(webapp.RequestHandler):
    def post(self):
        meal = Meal()

        if users.get_current_user():
            meal.author = users.get_current_user()
        meal.details = self.request.get('mealDetails')
        meal.name = self.request.get('mealName')
        meal.cost = self.request.get('mealCost')
        if meal.name:
            burgers = Meal.gql("WHERE mealDetails=:1",meal.details)
            if(burgers.count(5) == 0):
                meal.put()
                self.redirect('/')
        else:
            self.redirect('http://www.google.com');

class AddSmashBurger(webapp.RequestHandler):
    def post(self):
        user = SmashBurgerer()

        if users.get_current_user():
            user.user = users.get_current_user()
        user.name = self.request.get('name')
        if user.name:
            allUsers = SmashBurgerer.gql("WHERE user=:1",user.user)
            if(allUsers.count(5) == 0):
                user.put()
                self.redirect('/')       

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/home', MainPage),
                                      ('/about', MainPage),
                                      ('/addSmashBurgerer', AddSmashBurger),
                                      ('/addBurger', AddBurger)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()